import Vue from 'vue'
import Store from 'store'
import iView from 'iview'
import VueRouter from 'vue-router'
import Routers from './router'
import Utils from 'commons/utils'
import App from './app.vue'
import 'iview/dist/styles/iview.css'
import VueBus from 'vue-bus'
import CommonMixins from 'mixins/common_mixins'
require('plugins/http')
import utils from 'commons/utils'

Vue.use(VueRouter)
Vue.use(iView)
Vue.use(VueBus)

Vue.mixin(CommonMixins)

// 开启debug模式
Vue.config.debug = true

// 路由配置
const RouterConfig = {
    mode: 'hash',
    routes: Routers
};
const router = new VueRouter(RouterConfig)

const LOGIN_CHECK_EXCLUDE_PATHS = ['/login']  // 登录检查排除的路径
router.beforeEach((to, from, next) => {
    iView.LoadingBar.start()
    Utils.title(to.meta.title)
    if (utils.isLogin()) {
        if (to.path == '/login') {   // 如果已经登录，不允许跳转到登录页面
            next(to.query.from)
        } else {
            next()
        }
        return
    }
    if (LOGIN_CHECK_EXCLUDE_PATHS.indexOf(to.path) != -1) {
        next()
        return
    }

    let route = {
        path: '/login',
        query: {
            from: to.path
        }
    }
    next(route)
})

router.afterEach((to, from, next) => {
    iView.LoadingBar.finish()
    window.scrollTo(0, 0)
})

new Vue({
    el: '#app',
    router: router,
    store: Store,
    render: h => h(App)
})
